﻿using BaseTypesControlLib.Controls;
using System;
using System.Runtime.Serialization;
using System.Xml.Linq;
using System.Linq;

namespace BaseTypesLib.Model
{
    //derive from objectListField and override drawListItem or
    //add a list field and implement a callback
    public enum ListType { Object, Custom }
    [DataContract(IsReference = true)]
    public class CustomListField:ListField
    {
        private ListItemType itemType;
        private ListType listType;

        public CustomListField()
        {
            ListType = ListType.Custom;
        }
        [DataMember]
        public ListItemType ItemType
        {
            get { return itemType; }
            set
            {
                if (itemType != value)
                {
                    itemType = value;
                    OnPropertyChanged("ItemType");
                }
            }
        }
        [DataMember]
        public ListType ListType
        {
            get { return listType; }
            set
            {
                if (listType != value)
                {
                    listType = value;
                    OnPropertyChanged("ListType");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();

            el.SetAttributeValue("ListItemType", ItemType);
            el.SetAttributeValue("ListType", ListType);

            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var lit = elem.Attributes("ListItemType").FirstOrDefault();
            if (lit != null) ItemType = (ListItemType)Enum.Parse(typeof(ListItemType), lit.Value);
            var lt = elem.Attributes("ListType").FirstOrDefault();
            if (lt != null) ListType = (ListType)Enum.Parse(typeof(ListType), lt.Value);
        }
    }
}
